import {App, Builder, Configs, Database, Dev, Directorys, Env, UserData} from './types'

const path = require('path')
const fs   = require('fs')

/** 项目配置文件 */
let configFile = 'config/e3.config.cjs'

/** 加载配置文件 */
const loadConfig = (): Configs => {
    if (fs.existsSync(path.join(__dirname, '../../../..', configFile))) {
        return require(path.join(__dirname, '../../../..', configFile))
    }
    if (fs.existsSync(path.join(__dirname, '../../../../public', configFile))) {
        return require(path.join(__dirname, '../../../../public', configFile))
    }
    throw new Error(`请先定义配置文件 ${path.resolve(__dirname, '../../../..', configFile)}`)
}

/** config 初始化 */
const config: Configs = loadConfig()

/**
 * ex-electron-egg-config
 * ee-bin, ee-core 公共配置类
 */
class E3Config {
    
    /**
     * @type {E3Config} instance 单例
     * @private
     */
    private static instance: E3Config
    
    /**
     * @type {Configs} 配置
     * @private
     */
    private readonly configs: Configs
    
    /**
     * 私有构造函数防止外部实例化
     * @param {Configs} configs 配置
     * @private
     */
    private constructor(configs: Configs) {
        this.configs = configs
    }
    
    /**
     * 获取实例
     * @param {Configs} configs 配置
     * @returns {E3Config}
     */
    public static getInstance(configs: Configs): E3Config {
        if (!E3Config.instance) {
            E3Config.instance = new E3Config(configs)
        }
        return E3Config.instance
    }
    
    /**
     * 获取全部配置
     * @returns {Configs}
     */
    public getConfig(): Configs {
        return this.configs
    }
    
    /**
     * 获取目录配置
     * @returns {Directorys}
     */
    public getDirectorysConfig(): Directorys {
        return this.configs.directorys
    }
    
    /**
     * 获取环境配置
     * @returns {Env}
     */
    public getEnvConfig(): Env {
        return this.configs.env
    }
    
    /**
     * 获取开发配置
     * @returns {Dev}
     */
    public getDevConfig(): Dev {
        return this.configs.dev
    }
    
    /**
     * 获取App配置
     * @returns {App}
     */
    public getAppConfig(): App {
        return this.configs.app
    }
    
    /**
     * 获取打包配置
     * @returns {Builder}
     */
    public getBuilderConfig(): Builder {
        return this.configs.builder
    }
    
    /**
     * 获取用户数据配置
     * @returns {UserData}
     */
    public getUserDataConfig(): UserData {
        return this.configs.userData
    }
    
    /**
     * 获取数据库配置
     * @returns {Database}
     */
    public getDatabaseConfig(): Database {
        return this.configs.database
    }
}

/** e3 配置 */
const e3Config            = E3Config.getInstance(config)
const getConfig           = () => e3Config.getConfig()
const getDirectorysConfig = () => e3Config.getDirectorysConfig()
const getEnvConfig        = () => e3Config.getEnvConfig()
const getDevConfig        = () => e3Config.getDevConfig()
const getAppConfig        = () => e3Config.getAppConfig()
const getBuilderConfig    = () => e3Config.getBuilderConfig()
const getUserDataConfig   = () => e3Config.getUserDataConfig()
const getDatabaseConfig   = () => e3Config.getDatabaseConfig()

export {
    e3Config,
    getConfig,
    getDirectorysConfig,
    getEnvConfig,
    getDevConfig,
    getAppConfig,
    getBuilderConfig,
    getDatabaseConfig,
    getUserDataConfig
}
